﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using BusinessObjects.Models;
using EFRepository;

namespace ServerServices
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession
        , ConcurrencyMode = ConcurrencyMode.Single, MaxItemsInObjectGraph = 2147483647)]
    public partial class WcfEntryPoint : IAuthentificationService
    {
        public WcfEntryPoint(ManagerRepository repository)
        {
            Repository = repository;
        }

        public ManagerRepository Repository { get; set; }

        #region IAuthentificationService Members

        public bool ValidateUser(string login, string password)
        {
            return true;
        }

        public void Logoff(string user)
        {
            throw new NotImplementedException();
        }

        public void AddUser(User user)
        {
            Repository.AddUser(user);
        }

        public IEnumerable<User> GetAllUsersForDepartment(int departmentId)
        {
            return Repository.GetAllUsersForDepartment(departmentId);
        }

        public IEnumerable<Order> GetAllUsersOrders(int departmentId)
        {
            return Repository.GetAllUsersOrders(departmentId);
        }

        public Dictionary<Dish, int> GetAllUsersDishesFromDate(int departmentId, DateTime date)
        {
            return Repository.GetAllUsersDishesFromDate(departmentId, date);
        }

        public IEnumerable<Order> GetAllUsersOrdersFromDate(int departmentId, DateTime date)
        {
            return Repository.GetAllUsersOrdersFromDate(departmentId, date);
        }

        public void DeleteUser(User userInfo)
        {
            Repository.DeleteUser(userInfo);
        }

        public void UpdateUser(User userInfo)
        {
            Repository.UpdateUser(userInfo);
        }

        public void AddDishes(IEnumerable<Dish> dishes, int DepartmentId)
        {
            Repository.AddDishes(dishes, DepartmentId);
        }

        public void RemoveDishes(IEnumerable<Dish> dishes, int DepartmentId)
        {
            Repository.RemoveDishes(dishes, DepartmentId);
        }

        public void UpdateDishes(IEnumerable<Dish> dishes, int DepartmentId)
        {
            Repository.UpdateDishes(dishes, DepartmentId);
        }

        public IEnumerable<Dish> GetAllDishesForDepartment(int departmentId)
        {
            return Repository.GetAllDishesForDepartment(departmentId);
        }

        #endregion
    }

    public class ReaderQuotaExtension : IEndpointBehavior
    {
        #region Implementation of IEndpointBehavior

        public void Validate(ServiceEndpoint endpoint)
        {
        }

        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            ModifyDataContractSerializerBehavior(endpoint);
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            ModifyDataContractSerializerBehavior(endpoint);
        }

        private static void ModifyDataContractSerializerBehavior(ServiceEndpoint endpoint)
        {
            foreach (OperationDescription operation in endpoint.Contract.Operations)
            {
                var behavior = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
                behavior.MaxItemsInObjectGraph = 2147483647;
            }
        }

        #endregion
    }
}